#!/bin/sh
# -*- coding: utf-8 -*-
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: This agent plugin has been developed primarily for the Kubernetes use
# case (more details, see function definition). For this reason, it is
# NOT supported by the agent bakery.

# Reason for this no-op: shellcheck disable=... before the first command disables the error for the
# entire script.
:

# Disable unused variable error (needed to keep track of version)
# shellcheck disable=SC2034
CMK_VERSION="2.5.0b1"
CONFIG_FILE="${MK_CONFDIR}/lnx_container_host_if.cfg"

__read_network_interface_files() {

    path=${1}
    files=${2}

    for name in ${files}; do
        printf "%s\v%s\t" "${name}" "$(cat "${path}/${name}" 2>/dev/null)"
    done

}

section_lnx_container_host_if() {

    # This section essentially provides the same information as lnx_if, therefore
    # only one of these sections should be used. It is recommended to use lnx_if
    # (default in Checkmk).

    # lnx_if obtains its data from /proc/net/dev, this section obtains it from
    # /sys/class/net.

    # Use case: Kubernetes: The Checkmk agent runs inside a container on the
    # Kubernetes cluster but aims to collect information of the underlying node.
    # The node can be a real server, a VM, or a cloud server instance.
    # /proc/net/dev only shows interfaces of the host namespace (in this case
    # the container). Instead, the files in /sys/class/net are mounted from the
    # node into the container and provide network information of the node in this
    # way. Note that agent sections are sourced and run selectively, the lnx_if
    # section is therefore excluded.

    # HOST_PATH_PREFIX: This environment variable is used to provide a way to
    # mount /sys/class/net to an alternative path. If not set, the default location
    # is the system path (/sys/class/net).

    interface_filepath="${HOST_PATH_PREFIX}/sys/class/net"
    interface_statistics_subpath="statistics"

    network_interface_metadata="ifindex ifalias address type carrier"
    network_interface_speed="speed"
    network_interface_rx_stats="rx_bytes rx_packets rx_errors rx_dropped multicast"
    network_interface_tx_stats="tx_bytes tx_packets tx_errors tx_dropped tx_fifo_errors"

    echo "<<<lnx_container_host_if:sep(09)>>>"

    for if_path in "${interface_filepath}"/*; do

        # Check if Path is a directory, if not continue
        [ -d "${if_path}" ] || continue
        # Check if directory is empty, if true continue
        [ -n "$(ls -A "${if_path}")" ] || continue

        printf "name\v%s\t" "$(basename "${if_path}")"
        __read_network_interface_files "${if_path}" "${network_interface_metadata}"
        __read_network_interface_files "${if_path}" "${network_interface_speed}"
        __read_network_interface_files "${if_path}/${interface_statistics_subpath}" "${network_interface_rx_stats}"
        __read_network_interface_files "${if_path}/${interface_statistics_subpath}" "${network_interface_tx_stats}"
        printf "\n"

    done

}

main() {

    if [ -f "${CONFIG_FILE}" ]; then
        # shellcheck source=agents/cfg_examples/lnx_container_host_if.cfg
        . "${CONFIG_FILE}"
    fi

    section_lnx_container_host_if
}

[ -z "${MK_SOURCE_AGENT}" ] && main
